package com.pla.googlecalendarviews;

import java.net.URL;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.data.calendar.ColorProperty;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;

public class CalendarDAO {

  public static final String[] colors = {
    "#CC3333",
    "#DD4477",
    "#994499",
    "#6633CC",
    "#336699",
    "#3366CC",
    "#22AA99",
    "#329262",
    "#109618",
    "#66AA00",
    "#AAAA11",
    "#D6AE00",
    "#EE8800",
    "#DD5511",
    "#A87070",
    "#8C6D8C",
    "#627487",
    "#7083A8",
    "#5C8D87",
    "#898951",
    "#B08B59",
    "#A32929",
    "#B1365F",
    "#7A367A",
    "#5229A3",
    "#29527A",
    "#2952A3",
    "#1B887A",
    "#28754E",
    "#0D7813",
    "#528800",
    "#88880E",
    "#AB8B00",
    "#BE6D00",
    "#B1440E",
    "#865A5A",
    "#705770",
    "#4E5D6C",
    "#5A6986",
    "#4A716C",
    "#6E6E41",
    "#8D6F47"};

  public static void main(String[] args) throws Exception {
    CalendarDAO calendarDAO = new CalendarDAO();
    if (true) {
      calendarDAO.checkCalendarColors();
      System.exit(0);
    }
  }

  private void checkCalendarColors() throws Exception {
    Vector vector = new Vector();
    CalendarService service = new CalendarService(this.getClass().getName());
    service.setUserCredentials("patrick.archibald@hometelco.com", "linqlinq69");
    URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/allcalendars/full");
    CalendarFeed resultFeed = service.getFeed(feedUrl, CalendarFeed.class);
    for (int i = 0; i < resultFeed.getEntries().size(); i++) {
      CalendarEntry calendar = resultFeed.getEntries().get(i);
      for (int j = 0; j < colors.length; j++) {
        if (vector.contains(colors[j])) {
          System.out.println("Duplicate: " + colors[j]);
        }
        vector.add(colors[j]);
        ColorProperty colorProperty = calendar.getColor();
        System.out.println("Trying color: " + colors[j]);
        colorProperty.setValue(colors[j]);
        calendar.update();
      }
      System.out.println("All " + colors.length + " colors are OK");
      System.exit(0);
    }
  }

  public ArrayList<Calendar> getCalendars(String emailAddress, String password) throws Exception {
    ArrayList<Calendar> arrayList = new ArrayList<Calendar>();
    CalendarService service = new CalendarService(this.getClass().getName());
    service.setUserCredentials(emailAddress, password);
    URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/allcalendars/full");
    CalendarFeed resultFeed = service.getFeed(feedUrl, CalendarFeed.class);
    for (int i = 0; i < resultFeed.getEntries().size(); i++) {
      CalendarEntry calendarEntry = resultFeed.getEntries().get(i);
      Calendar calendarObject = new Calendar(calendarEntry);
      arrayList.add(calendarObject);
    }
    Collections.sort(arrayList);
    return arrayList;
  }

  public CalendarDAO() {
  }
}
